(() => {
    const $data = {
        cookie : null,
        status: true,
        delay: 3 * 1000
    }
    const $ws = new WebSocket('ws://localhost:8888/hook?id=B')
    const $post = (data) => {
        const message = { target: 'A', data }
        $ws.send(JSON.stringify(message))
    }
    setInterval(() => {
        if ($data.status) {
            const cookie = document.cookie
            if (cookie !== $data.cookie) {
                console.log('设置cookie')
                // 收到最新cookie发送给A端
                $post(cookie)
                $data.cookie = cookie
            }
        }
    }, $data.delay)
    $ws.onmessage = () => {
        // 当收到A端消息时代表cookie失效
        // 刷新游览器重新生成
        $data.status = false
        console.log('cookie失效')
        setTimeout(() => {
            window.location.reload(true)
        }, $data.delay)
    }
})()
